<?php
@ob_start();
@session_start();

include("../connect/connect.php");
include("../function/thaidate.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>-</title>
<link rel="stylesheet" href="stylereport.css" type="text/css" />
<link rel="shortcut icon" href="../image/Letter-T.ico" />
</head>
<body>
<?php
if($_POST['rtype']=='day' && $_POST['dateInput']!=""){
	$dmy = $_POST["dateInput"];
	$day = substr($dmy, 0, 2);
	$month = substr($dmy, 3, 2);
	$y = substr($dmy, 6, 4);
	$year = $y-543;
	//$date = ($y - 543) . '-' . $m . '-' . $d;
	//$date = date("Y-m-d", $adate);

//แสดงรายงานเครื่องดื่มแอลกอฮอล์
	$adate = $year . '-' . $month . '-' . $day;
	
$Query = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '3' AND day(sale.sale_datetime3) = '$day' AND month(sale.sale_datetime3) = '$month' AND year(sale.sale_datetime3) = '$year' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query1 = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '2' AND day(sale.sale_datetime3) = '$day' AND month(sale.sale_datetime3) = '$month' AND year(sale.sale_datetime3) = '$year' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query3 = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '1' AND day(sale.sale_datetime3) = '$day' AND month(sale.sale_datetime3) = '$month' AND year(sale.sale_datetime3) = '$year' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query4 = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '4' AND day(sale.sale_datetime3) = '$day' AND month(sale.sale_datetime3) = '$month' AND year(sale.sale_datetime3) = '$year' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");

}else if($_POST['rtype']=='month' && $_POST['monthSelect']!="" && $_POST['yearSelect']!=""){
	
$Query = 	mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '3' AND month(sale.sale_datetime3) = '".$_POST['monthSelect']."' AND year(sale.sale_datetime3) = '".$_POST['yearSelect']."' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query1 = 	mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '2' AND month(sale.sale_datetime3) = '".$_POST['monthSelect']."' AND year(sale.sale_datetime3) = '".$_POST['yearSelect']."' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query3 = 	mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '1' AND month(sale.sale_datetime3) = '".$_POST['monthSelect']."' AND year(sale.sale_datetime3) = '".$_POST['yearSelect']."' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query4 = 	mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '4' AND month(sale.sale_datetime3) = '".$_POST['monthSelect']."' AND year(sale.sale_datetime3) = '".$_POST['yearSelect']."' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");

}else if($_POST['rtype']=='dateto' && $_POST['dateInput1']!="" && $_POST['dateInput2']!=""){
	
	$dmy = $_POST["dateInput1"];
	$day = substr($dmy, 0, 2);
	$month = substr($dmy, 3, 2);
	$y = substr($dmy, 6, 4);
	$year = $y-543;
	$date1 = $year."-".$month."-".$day;
	
	$dmy1 = $_POST["dateInput2"];
	$day1 = substr($dmy1, 0, 2);
	$month1 = substr($dmy1, 3, 2);
	$y1 = substr($dmy1, 6, 4);
	$year1 = $y1-543;
	$date2 = $year1."-".$month1."-".$day1;
	
$Query = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '3' AND sale.sale_datetime3 BETWEEN '$date1' AND '$date2' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query1 = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '2' AND sale.sale_datetime3 BETWEEN '$date1' AND '$date2' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query3 = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '1' AND sale.sale_datetime3 BETWEEN '$date1' AND '$date2' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query4 = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '4' AND sale.sale_datetime3 BETWEEN '$date1' AND '$date2' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
}
?>
<font size="5"><strong>รายงานการขายแยกตามประเภทสินค้า</strong></font><br><br>
<font size="4"><strong><?php 
if($_POST['rtype']=='day'){
	echo "ประจำ".thaidate(strtotime($adate));
} else if($_POST['rtype']=='month'){
	$monthname = array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
	$year = $_POST['yearSelect']+543;
	echo "ประจำเดือน ".$monthname[$_POST['monthSelect']-1]." พ.ศ. ". $year ;
} else if($_POST['rtype']=='dateto'){
	echo "ตั้งแต่วันที่ ".thai_date(strtotime($date1))." ถึงวันที่ ".thai_date(strtotime($date2));
}
 ?></strong></font><br>
<h2>เครื่องดื่มแอลกอฮอล์</h2>
<table class="listorder">
  <thead>
<tr>
<td width="5%">ลำดับ</td>
<td>รายการ</td>
<td width="12%">หน่วยละ</td>
<td width="9%">จำนวน</td>
<td width="15%">ราคาขาย</td>
<td width="15%">ราคา + Sv.</td>
</tr>
  </thead>
<?php
$num1 = 0;
while($row1 = mysql_fetch_array($Query)) {
	$num1++;
?>
<tr>
<td align="center"><?php echo $num1 ?></td>
<td align="left" style="padding-left:10px"><?php echo $row1['prod_name']; ?></td>
<td align="right" style="padding-right:10px"><?php echo "".number_format($row1['sale_price'], 2,'.',','); ?></td>
<td align="right" style="padding-right:15px"><?php echo $row1['sale_qty']; ?></td>
<td align="right" style="padding-right:10px"><?php echo "".number_format($row1['sale_price']*$row1['sale_qty'], 2,'.',','); ?></td>
<td align="right" style="padding-right:10px">
<?php
$sale_discount = ($row1['sale_price']*$row1['sale_qty'])+$row1['sale_discount'];
echo "".number_format($sale_discount, 2,'.',',');
?></td>
</tr>
<?php
	$strTotal1=$strTotal1+$sale_discount;
}
?>
<tfoot>
  <tr>
    <td colspan="5" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:10px"><?php echo "".number_format($strTotal1, 2,'.',','); ?></td>
	</tr>
</tfoot>
</table>


<!-- แสดงรายงานมิกเซอร์ -->
<h2>มิกเซอร์</h2>
<table class="listorder">
  <thead>
<tr>
<td width="5%">ลำดับ</td>
<td>รายการ</td>
<td width="12%">หน่วยละ</td>
<td width="9%">จำนวน</td>
<td width="15%">ราคาขาย</td>
<td width="15%">ราคา + Sv.</td>
</tr>
  </thead>
<?php
$num2 = 0;
while($row2 = mysql_fetch_array($Query1)) {
	$num2++;
?>
<tr>
<td align="center"><?php echo $num2 ?></td>
<td align="left" style="padding-left:10px"><?php echo $row2['prod_name']; ?></td>
<td align="right" style="padding-right:10px"><?php echo "".number_format($row2['sale_price'], 2,'.',','); ?></td>
<td align="right" style="padding-right:15px"><?php echo $row2['sale_qty']; ?></td>
<td align="right" style="padding-right:10px"><?php echo "".number_format($row2['sale_price']*$row2['sale_qty'], 2,'.',','); ?></td>
<td align="right" style="padding-right:10px"><?php $sale_discount2 = ($row2['sale_price']*$row2['sale_qty'])+($row2['sale_discount']); 
echo "".number_format($sale_discount2, 2,'.',',');
?></td>
</tr>
<?php
	$strTotal2=$strTotal2+$sale_discount2;
}
?>
<tfoot>
  <tr>
    <td colspan="5" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:10px"><?php echo "".number_format($strTotal2, 2,'.',','); ?></td>
	</tr>
</tfoot>
</table>


<!-- แสดงรายงานอาหาร -->
<h2>อาหาร</h2>
<table class="listorder">
  <thead>
<tr>
<td width="5%">ลำดับ</td>
<td>รายการ</td>
<td width="12%">หน่วยละ</td>
<td width="9%">จำนวน</td>
<td width="15%">ราคาขาย</td>
<td width="15%">ราคา + Sv.</td>
</tr>
  </thead>
<?php
$num3 = 0;
while($row3 = mysql_fetch_array($Query3)) {
	$num3++;
?>
<tr>
<td align="center"><?php echo $num3 ?></td>
<td align="left" style="padding-left:10px"><?php echo $row3['prod_name']; ?></td>
<td align="right" style="padding-right:10px"><?php echo "".number_format($row3['sale_price'], 2,'.',','); ?></td>
<td align="right" style="padding-right:15px"><?php echo $row3['sale_qty']; ?></td>
<td align="right" style="padding-right:10px"><?php echo "".number_format($row3['sale_price']*$row3['sale_qty'], 2,'.',','); ?></td>
<td align="right" style="padding-right:10px"><?php $sale_discount3 = ($row3['sale_price']*$row3['sale_qty'])+($row3['sale_discount']);
echo "".number_format($sale_discount3, 2,'.',',');
?></td>
</tr>
<?php
	$strTotal3=$strTotal3+$sale_discount3;
}
?>
<tfoot>
  <tr>
    <td colspan="5" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:10px"><?php echo "".number_format($strTotal3, 2,'.',','); ?></td>
	</tr>
</tfoot>
</table>

<!-- แสดงรายงานอื่นๆ -->
<h2>อื่นๆ</h2>
<table class="listorder">
  <thead>
<tr>
<td width="5%">ลำดับ</td>
<td>รายการ</td>
<td width="12%">หน่วยละ</td>
<td width="9%">จำนวน</td>
<td width="15%">ราคาขาย</td>
<td width="15%">ราคา + Sv.</td>
</tr>
  </thead>
<?php
$num4 = 0;
while($row4 = mysql_fetch_array($Query4)) {
	$num4++;
?>
<tr>
<td align="center"><?php echo $num4; ?></td>
<td align="left" style="padding-left:10px"><?php echo $row4['prod_name']; ?></td>
<td align="right" style="padding-right:10px"><?php echo "".number_format($row4['sale_price'], 2,'.',','); ?></td>
<td align="right" style="padding-right:15px"><?php echo $row4['sale_qty']; ?></td>
<td align="right" style="padding-right:10px"><?php echo "".number_format($row4['sale_price']*$row4['sale_qty'], 2,'.',','); ?></td>
<td align="right" style="padding-right:10px"><?php $sale_discount4 = ($row4['sale_price']*$row4['sale_qty'])+($row4['sale_discount']);
echo "".number_format($sale_discount4, 2,'.',',');
?></td>
</tr>
<?php
	$strTotal4=$strTotal4+$sale_discount4;
}
?>
<tfoot>
  <tr>
    <td colspan="5" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:10px"><?php echo "".number_format($strTotal4, 2,'.',','); ?></td>
	</tr>
</tfoot>
</table>

<div class="endtotal">รวมทั้งหมด <?php echo "".number_format($strTotal1+$strTotal2+$strTotal3+$strTotal4, 2,'.',',')." บาท"; ?></div>
<div style="padding-top: 20px">
<input type="button" name="endsale" id="endsale" value="พิมพ์รายงาน" onclick="javascript:window.print();" > <input type="button" name="btnClose" value="ปิดหน้าต่าง" onclick="JavaScript:window.close();" /></div>
</body>

</html>
